package com.perye.dokit.service.impl;

import cn.hutool.core.util.IdUtil;
import com.perye.dokit.dto.WeWeddingStyleDto;
import com.perye.dokit.entity.WeWeddingStyle;
import com.perye.dokit.mapper.WeWeddingStyleMapper;
import com.perye.dokit.mapper.dao.DaoSupport;
import com.perye.dokit.query.WeWeddingStyleQueryCriteria;
import com.perye.dokit.repository.WeWeddingStyleRepository;
import com.perye.dokit.service.WeWeddingStyleService;
import com.perye.dokit.utils.FileUtil;
import com.perye.dokit.utils.PageUtil;
import com.perye.dokit.utils.QueryHelp;
import com.perye.dokit.utils.ValidationUtil;
import com.perye.dokit.utils.pageData.PageData;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

@Service
//@CacheConfig(cacheNames = "weWeddingStyle")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class WeWeddingStyleServiceImpl implements WeWeddingStyleService {

    private final WeWeddingStyleRepository weWeddingStyleRepository;

    private final WeWeddingStyleMapper weWeddingStyleMapper;
    @Resource(name = "daoSupport")
    private DaoSupport dao;
    @Override
    public void insterImg(PageData pageData)throws Exception {
        dao.save("weWeddingStyleMapper.insertVideoPicture",pageData);
    }

    @Override
    public void delWeVideoPicture(PageData pageData) throws Exception {
        dao.update("weWeddingStyleMapper.delWeVideoPicture",pageData);
    }

    @Override
    public List<PageData> selectWeddingStyle(PageData pageData) throws Exception {
        return (List<PageData>) dao.findForList("weWeddingStyleMapper.selectWeddingStyle", pageData);
    }

    public WeWeddingStyleServiceImpl(WeWeddingStyleRepository weWeddingStyleRepository, WeWeddingStyleMapper weWeddingStyleMapper) {
        this.weWeddingStyleRepository = weWeddingStyleRepository;
        this.weWeddingStyleMapper = weWeddingStyleMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(WeWeddingStyleQueryCriteria criteria, Pageable pageable){
        Page<WeWeddingStyle> page = weWeddingStyleRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(weWeddingStyleMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<WeWeddingStyleDto> queryAll(WeWeddingStyleQueryCriteria criteria){
        return weWeddingStyleMapper.toDto(weWeddingStyleRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public WeWeddingStyleDto findById(String id) {
        WeWeddingStyle weWeddingStyle = weWeddingStyleRepository.findById(id).orElseGet(WeWeddingStyle::new);
        ValidationUtil.isNull(weWeddingStyle.getId(),"WeWeddingStyle","id",id);
        return weWeddingStyleMapper.toDto(weWeddingStyle);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public WeWeddingStyleDto create(WeWeddingStyle resources) {

        return weWeddingStyleMapper.toDto(weWeddingStyleRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(WeWeddingStyle resources) {
        WeWeddingStyle weWeddingStyle = weWeddingStyleRepository.findById(resources.getId()).orElseGet(WeWeddingStyle::new);
        ValidationUtil.isNull( weWeddingStyle.getId(),"WeWeddingStyle","id",resources.getId());
        weWeddingStyle.copy(resources);
        weWeddingStyleRepository.save(weWeddingStyle);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            weWeddingStyleRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<WeWeddingStyleDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (WeWeddingStyleDto weWeddingStyle : all) {
            Map<String,Object> map = new LinkedHashMap<>();
                         map.put(" typeId",  weWeddingStyle.getTypeId());
                         map.put(" name",  weWeddingStyle.getName());
                         map.put(" price",  weWeddingStyle.getPrice());
                         map.put(" deposit",  weWeddingStyle.getDeposit());
                         map.put(" serviceDetails",  weWeddingStyle.getServiceDetails());
                         map.put(" priceRecommend",  weWeddingStyle.getPriceRecommend());
                         map.put(" workRecommend",  weWeddingStyle.getWorkRecommend());
                         map.put(" isOpen",  weWeddingStyle.getIsOpen());
                         map.put(" createTime",  weWeddingStyle.getCreateTime());
                         map.put(" updateTime",  weWeddingStyle.getUpdateTime());
                         map.put(" isDelete",  weWeddingStyle.getIsDelete());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

 }
